<template>
	<view class="">
		<j-cell-group>
			<radio-group @change="radioChange">
				<j-cell title="全天报警" :more="false">
					<view slot="icon">
						<radio style="margin-left: 10rpx;" value="all" :checked="checkRadio == 0" />
					</view>
				</j-cell>
				<j-cell-card title="自定义报警" :show="customShow" :is-touch="customShow" :more="customShow" border style="background: #f5f7fb">
					<view slot="icon">
						<radio style="margin-left: 10rpx;" value="custom" :checked="checkRadio == 1" />
					</view>
					<j-cell v-if="DetectMotionDetectList.length > 0" @jftap="toTiming(item,index)" :more="true"
						v-for="(item,index) in 5" :key="index">
						<view slot="icon" style="font-size: 24rpx;margin-left: 10rpx;">
							<text>时间段{{index+1}}</text> <text
								style="margin-left: 10rpx;">{{DetectMotionDetectList[0].EventHandler.TimeSection[0][index+1] | timeFilters}}</text>
						</view>
						<view slot="title" style="font-size: 24rpx;margin-left: 20rpx;">
							<view class="weeklist">
								<p style="text-align: center;">
									{{getweek(timeQuantum[index],'1')}}
								</p>
								<p style="text-align: center;"> {{getweek(timeQuantum[index],'2')}}</p>
							</view>
						</view>
					</j-cell>
				</j-cell-card>
			</radio-group>
		</j-cell-group>
		<view class="bottom-fixed">
			<button type="primary" class="block" plain @tap="setconfig">保存</button>
		</view>
	</view>
</template>

<script>
	export default {
		filters: {
			timeFilters(oldValue) {
				return oldValue.substr(2) //返回处理过的值
			},
		},
		data() {
			return {
				radio: 'all',
				checkRadio:0,
				DetectMotionDetectList: [],
				weeks: [{
					name: '星期日',
					status: false
				}, {
					name: '星期一',
					status: false
				}, {
					name: '星期二',
					status: false
				}, {
					name: '星期三',
					status: false
				}, {
					name: '星期四',
					status: false
				}, {
					name: '星期五',
					status: false
				}, {
					name: '星期六',
					status: false
				}, ],
				timeMaskList: [],
				device: {},
				token: '',
				timeQuantum: [],
				customShow:false
			}
		},
		onLoad(options) {
			console.log(options)
			this.device = options
			this.token = options.token
			this.getconfig();
		},
		methods: {
			toTiming(v, index) {
				uni.navigateTo({
					url: '/jvss/pagesDevice/device/device-live/setting/IPolice/timing?token=' + this.token +
						'&timingType=' + v
				});
			},
			radioChange(v) {
				this.DetectMotionDetectList[0].EventHandler.TimeSection.forEach(item => {
					if (v.detail.value == 'all') {
						item[0] = item[0].replace(item[0].substring(0, 1), '1')
						this.customShow = false
					} else {
						item[0] = item[0].replace(item[0].substring(0, 1), '0')
						this.customShow = true
					}
				})
			},
			getconfig() {
				uni.showLoading();
				let token = this.token;
				let data = {
					Name: 'Detect.MotionDetect'
				};
				getApp().globalData.api.getconfig(data, token, (result) => {
					uni.hideLoading()
					console.log(result);
					this.DetectMotionDetectList = result['Detect.MotionDetect']
					this.weekFilter(result['Detect.MotionDetect'][0].EventHandler.TimeSection)
					result['Detect.MotionDetect'][0].EventHandler.TimeSection.forEach(item => {
						if (item[0].substring(0, 1) == '1') {
							this.radio = 'all'
							this.checkRadio = 0;
							
						} else {
							this.radio = 'custom'
							this.checkRadio = 1;
							this.customShow = true
						}
					})
					this.$forceUpdate()
				});
			},
			// 星期间隔过滤
			weekFilter(value) {
				let arr = []
				let arr2 = []
				let arr3 = []
				let arr4 = []
				let arr5 = []
				let arr6 = []
				arr2 = value.map((item, index) => {
					arr = item.map((ele, indez) => {
						ele = ele.slice(0, 1)
						return ele
					})
					arr.shift()
					return arr
				})
				let timeArr = ['时间段1', '时间段2', '时间段3', '时间段4', '时间段5']
				arr4 = arr2.map((ele, index) => {
					arr3 = timeArr.map((item, indez) => {
						let obj = {
							name: '',
							status: null
						}
						obj.name = item
						obj.status = ele[indez]
						return obj
					})
					return arr3
				})
				arr6 = timeArr.map((ele, index) => {
					arr5 = arr4.map(item => {
						let status = null
						if (item[index].name == ele) {
							status = item[index].status
						}
						return status
					})
					return arr5
				})
				this.timeQuantum = arr6
			},
			// 星期时间段状态过滤
			getweek(v, type) {
				let weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
				let weeksName = '';
				let status = null
				v.forEach((ele, indez) => {
					if (ele == '1') {
						weeksName += weeks[indez]
					}
				})
				let a = v.filter(e => e == '0')
				if (a.length == 7) {
					status = '未开启'
				} else if (a.length > 0) {
					status = '已开启'
				} else if (a.length == 0) {
					status = '已开启'
					weeksName = '每天'
				}
				if (type == '1') return weeksName
				if (type == '2') return status
			},

			// 设置配置
			setconfig() {
				uni.showLoading();
				let token = this.token;
				let data = {
					Name: 'Detect.MotionDetect',
					'Detect.MotionDetect': this.DetectMotionDetectList
				};
				getApp().globalData.api.setconfig(data, token, (result) => {
					uni.hideLoading()
					uni.navigateBack({
						delta: 1
					});
					var pages = getCurrentPages();
					var prePage = pages[pages.length - 2] //上一个页面
					console.log(prePage)
					if (prePage.route == "jvss/pagesDevice/device/device-live/setting/IPolice/callThePolice") {
						console.log(prePage.$vm)
						prePage.$vm.getconfig('Detect.MotionDetect');
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.weeklist {

		width: 280rpx;
	}
</style>
